package com.sxx.beautifulcity.adapter;

import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.entity.greendao.BarbecuteDaoEntity;
import com.sxx.beautifulcity.listenerInterface.OnBarbecuteItemClickListener;
import com.sxx.beautifulcity.listenerInterface.OnLineItemClickListener;

import java.util.List;

/**
 * Created by dllo on 15/10/31.
 * 搜索界面烧烤
 */
public class BarbecueAdapter extends RecyclerView.Adapter<BarbecueAdapter.BarbecueAdapterViewHolder>{

    private List<BarbecuteDaoEntity> list;
    private OnBarbecuteItemClickListener listener;

    public void OnBarbecuteItemClickListener(OnBarbecuteItemClickListener listener){
        this.listener = listener;
    }

    public BarbecueAdapter() {

    }

    public  void  addData(List<BarbecuteDaoEntity> list){
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public BarbecueAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.activity_barbecue_item , null);
        return new BarbecueAdapterViewHolder(v);
    }

    @Override
    public void onBindViewHolder(BarbecueAdapterViewHolder holder, int position) {
        if (list != null && list.size() > 0) {
            holder.barbecueName.setText(list.get(position).getName());
            holder.barbecueRatingBar.setNumStars(list.get(position).getStar());
            String path = list.get(position).getImage();
            requestImage(path , holder.barbecueImage);
        }
        holder.position = position;
    }

    public void requestImage(String path , SimpleDraweeView draweeView){
        Uri uri = Uri.parse(path);
        draweeView.setImageURI(uri);
    }

    @Override
    public int getItemCount() {
        return list != null && list.size() > 0 ? list.size() : 0;
    }

    class BarbecueAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView barbecueName;
        private SimpleDraweeView barbecueImage;
        private Button editBtn;
        private RatingBar barbecueRatingBar;
        private int position;

        public BarbecueAdapterViewHolder(View itemView) {
            super(itemView);
            barbecueName = (TextView) itemView.findViewById(R.id.barbecueName);
            barbecueImage = (SimpleDraweeView) itemView.findViewById(R.id.barbecueImage);
            editBtn = (Button) itemView.findViewById(R.id.editBtn);
            barbecueRatingBar = (RatingBar) itemView.findViewById(R.id.barbecueRatingBar);
            barbecueImage.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (list != null && list.size() > 0) {
                BarbecuteDaoEntity entity = list.get(position);
                if (listener != null) {
                    listener.OnBarbecuteItemClick(entity);
                }
            }
        }
    }

}
